/*
 * This file is part of Cadmium.
 * Copyright (C) 2007-2010 Xavier Clerc.
 *
 * Cadmium is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Cadmium is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package fr.x9c.cadmium.primitives.cadmiumxml;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.validation.Schema;

import org.w3c.dom.Attr;
import org.w3c.dom.CDATASection;
import org.w3c.dom.Comment;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentType;
import org.w3c.dom.Element;
import org.w3c.dom.Entity;
import org.w3c.dom.EntityReference;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Notation;
import org.w3c.dom.ProcessingInstruction;
import org.w3c.dom.Text;

import fr.x9c.cadmium.kernel.Block;
import fr.x9c.cadmium.kernel.Channel;
import fr.x9c.cadmium.kernel.CodeRunner;
import fr.x9c.cadmium.kernel.Fail;
import fr.x9c.cadmium.kernel.Primitive;
import fr.x9c.cadmium.kernel.PrimitiveProvider;
import fr.x9c.cadmium.kernel.Value;
import fr.x9c.cadmium.primitives.cadmium.Cadmium;

/**
 * This class provides the primitives for document manipulation.
 *
 * @author <a href="mailto:cadmium@x9c.fr">Xavier Clerc</a>
 * @version 1.0
 * @since 1.0
 */
@PrimitiveProvider
public final class Documents {

    /**
     * No instance of this class.
     */
    private Documents() {
    } // end empty constructor

    /**
     * Returns the root element of the document.
     * @param ctxt context
     * @param doc document
     * @return the root element of the passed document
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_get_document_element(final CodeRunner ctxt,
                                                                 final Value doc)
        throws Fail.Exception {
        try {
            final Document d = (Document) doc.asBlock().asCustom();
            return Cadmium.createObject(d.getDocumentElement());
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_get_document_element(CodeRunner, Value)'

    /**
     * Returns the document encoding.
     * @param ctxt context
     * @param doc document
     * @return the encoding used for the passed document
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_get_xml_encoding(final CodeRunner ctxt,
                                                             final Value doc)
        throws Fail.Exception {
        try {
            final Document d = (Document) doc.asBlock().asCustom();
            final String res = d.getXmlEncoding();
            return Value.createFromBlock(Block.createString(res));
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_get_xml_encoding(CodeRunner, Value)'

    /**
     * Sets the 'scrict-error-checking' property for passed document.
     * @param ctxt context
     * @param doc document
     * @param value value for 'scrict-error-checking' property
     * @return <i>unit</i>
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_set_strict_error_checking(final CodeRunner ctxt,
                                                                      final Value doc,
                                                                      final Value value)
        throws Fail.Exception {
        try {
            final Document d = (Document) doc.asBlock().asCustom();
            d.setStrictErrorChecking(value == Value.TRUE);
            return Value.UNIT;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_set_strict_error_checking(CodeRunner, Value, Value)'

    /**
     * Gets the 'scrict-error-checking' property for passed document.
     * @param ctxt context
     * @param doc document
     * @return value for 'scrict-error-checking' property
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_is_strict_error_checking(final CodeRunner ctxt,
                                                                     final Value doc)
        throws Fail.Exception {
        try {
            final Document d = (Document) doc.asBlock().asCustom();
            return d.getStrictErrorChecking() ? Value.TRUE : Value.FALSE;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_is_strict_error_checking(CodeRunner, Value)'

    /**
     * Sets the 'xml-standlone' property for a document.
     * @param ctxt context
     * @param doc document
     * @param value value for 'xml-standlone' property
     * @return <i>unit</i>
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_set_xml_standalone(final CodeRunner ctxt,
                                                               final Value doc,
                                                               final Value value)
        throws Fail.Exception {
        try {
            final Document d = (Document) doc.asBlock().asCustom();
            d.setXmlStandalone(value == Value.TRUE);
            return Value.UNIT;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_set_xml_standalone(CodeRunner, Value, Value)'

    /**
     * Gets the 'xml-standlone' property for passed document.
     * @param ctxt context
     * @param doc document
     * @return value for 'xml-standlone' property
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_is_xml_standalone(final CodeRunner ctxt,
                                                              final Value doc)
        throws Fail.Exception {
        try {
            final Document d = (Document) doc.asBlock().asCustom();
            return d.getXmlStandalone() ? Value.TRUE : Value.FALSE;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_is_xml_standalone(CodeRunner, Value)'

    /**
     * Sets the 'xml-version' property for a document.
     * @param ctxt context
     * @param doc document
     * @param value value for 'xml-version' property
     * @return <i>unit</i>
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_set_xml_version(final CodeRunner ctxt,
                                                            final Value doc,
                                                            final Value value)
        throws Fail.Exception {
        try {
            final Document d = (Document) doc.asBlock().asCustom();
            d.setXmlVersion(value.asBlock().asString());
            return Value.UNIT;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_set_xml_version(CodeRunner, Value, Value)'

    /**
     * Gets the 'xml-version' property for passed document.
     * @param ctxt context
     * @param doc document
     * @return value for 'xml-version' property
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_get_xml_version(final CodeRunner ctxt,
                                                            final Value doc)
        throws Fail.Exception {
        try {
            final Document d = (Document) doc.asBlock().asCustom();
            final String res = d.getXmlVersion();
            return Value.createFromBlock(Block.createString(res));
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_get_xml_version(CodeRunner, Value)'

    /**
     * Normalizes a document.
     * @param ctxt context
     * @param doc document to normalize
     * @return <i>unit</i>
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_normalize_document(final CodeRunner ctxt,
                                                               final Value doc)
        throws Fail.Exception {
        try {
            final Document d = (Document) doc.asBlock().asCustom();
            d.normalize();
            return Value.UNIT;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_normalize_document(CodeRunner, Value)'

    /**
     * Returns the parent of the passed attribute.
     * @param ctxt context
     * @param attr attribute
     * @return the parent of the passed attribute
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_get_attr_parent(final CodeRunner ctxt,
                                                            final Value attr)
        throws Fail.Exception {
        try {
            final Attr a = (Attr) attr.asBlock().asCustom();
            return Cadmium.createObject(a.getOwnerElement());
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_get_attr_parent(CodeRunner, Value)'

    /**
     * Returns the name of the passed attribute.
     * @param ctxt context
     * @param attr attribute
     * @return the name of the passed attribute
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_get_attr_name(final CodeRunner ctxt,
                                                          final Value attr)
        throws Fail.Exception {
        try {
            final Attr a = (Attr) attr.asBlock().asCustom();
            final String res = a.getName();
            return Value.createFromBlock(Block.createString(res));
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_get_attr_name(CodeRunner, Value)'

    /**
     * Returns the value of the passed attribute.
     * @param ctxt context
     * @param attr attribute
     * @return the value of the passed attribute
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_get_attr_value(final CodeRunner ctxt,
                                                           final Value attr)
        throws Fail.Exception {
        try {
            final Attr a = (Attr) attr.asBlock().asCustom();
            final String res = a.getValue();
            return Value.createFromBlock(Block.createString(res));
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_get_attr_value(CodeRunner, Value)'

    /**
     * Changes the value of the passed attribute.
     * @param ctxt context
     * @param attr attribute
     * @param value new value
     * @return <i>unit</i>
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_set_attr_value(final CodeRunner ctxt,
                                                           final Value attr,
                                                           final Value value)
        throws Fail.Exception {
        try {
            final Attr a = (Attr) attr.asBlock().asCustom();
            a.setValue(value.asBlock().asString());
            return Value.UNIT;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_set_attr_value(CodeRunner, Value, Value)'

    /**
     * Returns the parent of the passed cdata.
     * @param ctxt context
     * @param cdata cdata
     * @return the parent of the passed cdata
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_get_cdata_parent(final CodeRunner ctxt,
                                                             final Value cdata)
        throws Fail.Exception {
        try {
            final CDATASection c = (CDATASection) cdata.asBlock().asCustom();
            return createNode(c.getParentNode());
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_get_cdata_parent(CodeRunner, Value)'

    /**
     * Returns the data of the passed cdata.
     * @param ctxt context
     * @param cdata cdata
     * @return the data of the passed cdata
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_get_cdata(final CodeRunner ctxt,
                                                      final Value cdata)
        throws Fail.Exception {
        try {
            final CDATASection c = (CDATASection) cdata.asBlock().asCustom();
            final String res = c.getData();
            return Value.createFromBlock(Block.createString(res));
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_get_cdata(CodeRunner, Value)'

    /**
     * Changes the data of the passed cdata.
     * @param ctxt context
     * @param cdata cdata
     * @param value new data
     * @return <i>unit</i>
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_set_cdata(final CodeRunner ctxt,
                                                      final Value cdata,
                                                      final Value value)
        throws Fail.Exception {
        try {
            final CDATASection c = (CDATASection) cdata.asBlock().asCustom();
            c.setData(value.asBlock().asString());
            return Value.UNIT;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_set_cdata(CodeRunner, Value, Value)'

    /**
     * Returns the parent of the passed comment.
     * @param ctxt context
     * @param comment comment
     * @return the parent of the passed comment
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_get_comment_parent(final CodeRunner ctxt,
                                                               final Value comment)
        throws Fail.Exception {
        try {
            final Comment c = (Comment) comment.asBlock().asCustom();
            return createNode(c.getParentNode());
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_get_comment_parent(CodeRunner, Value)'

    /**
     * Returns the data of the passed comment.
     * @param ctxt context
     * @param comment comment
     * @return the data of the passed comment
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_get_comment(final CodeRunner ctxt,
                                                        final Value comment)
        throws Fail.Exception {
        try {
            final Comment c = (Comment) comment.asBlock().asCustom();
            final String res = c.getData();
            return Value.createFromBlock(Block.createString(res));
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_get_comment(CodeRunner, Value)'

    /**
     * Changes the data of the passed comment.
     * @param ctxt context
     * @param comment comment
     * @param value new data
     * @return <i>unit</i>
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_set_comment(final CodeRunner ctxt,
                                                        final Value comment,
                                                        final Value value)
        throws Fail.Exception {
        try {
            final Comment c = (Comment) comment.asBlock().asCustom();
            c.setData(value.asBlock().asString());
            return Value.UNIT;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_set_comment(CodeRunner, Value, Value)'

    /**
     * Returns the parent of the passed document type.
     * @param ctxt context
     * @param doctype document type
     * @return the parent of the passed document type
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_get_document_type_parent(final CodeRunner ctxt,
                                                                     final Value doctype)
        throws Fail.Exception {
        try {
            final DocumentType dt = (DocumentType) doctype.asBlock().asCustom();
            return createNode(dt.getParentNode());
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_get_document_type_parent(CodeRunner, Value)'

    /**
     * Returns the name of the passed document type.
     * @param ctxt context
     * @param doctype document type
     * @return the name of the passed document type
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_get_document_type_name(final CodeRunner ctxt,
                                                                   final Value doctype)
        throws Fail.Exception {
        try {
            final DocumentType dt = (DocumentType) doctype.asBlock().asCustom();
            final String res = dt.getName();
            return Value.createFromBlock(Block.createString(res));
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_get_document_type_name(CodeRunner, Value)'

    /**
     * Returns the public ID of the passed document type.
     * @param ctxt context
     * @param doctype document type
     * @return the public ID of the passed document type
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_get_document_type_public_id(final CodeRunner ctxt,
                                                                        final Value doctype)
        throws Fail.Exception {
        try {
            final DocumentType dt = (DocumentType) doctype.asBlock().asCustom();
            final String res = dt.getPublicId();
            return Value.createFromBlock(Block.createString(res));
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_get_document_type_public_id(CodeRunner, Value)'

    /**
     * Returns the system ID of the passed document type.
     * @param ctxt context
     * @param doctype document type
     * @return the system ID of the passed document type
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_get_document_type_system_id(final CodeRunner ctxt,
                                                                        final Value doctype)
        throws Fail.Exception {
        try {
            final DocumentType dt = (DocumentType) doctype.asBlock().asCustom();
            final String res = dt.getSystemId();
            return Value.createFromBlock(Block.createString(res));
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_get_document_type_system_id(CodeRunner, Value)'

    /**
     * Returns the parent of the passed element.
     * @param ctxt context
     * @param element element
     * @return the parent of the passed element
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_get_element_parent(final CodeRunner ctxt,
                                                               final Value element)
        throws Fail.Exception {
        try {
            final Element e = (Element) element.asBlock().asCustom();
            return createNode(e.getParentNode());
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_get_element_parent(CodeRunner, Value)'

    /**
     * Returns the value of an attribute.
     * @param ctxt context
     * @param element to get attribute from
     * @param name attribute
     * @return value of given attribute for passed element
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_get_attribute(final CodeRunner ctxt,
                                                          final Value element,
                                                          final Value name)
        throws Fail.Exception {
        try {
            final Element e = (Element) element.asBlock().asCustom();
            final String res = e.getAttribute(name.asBlock().asString());
            return Value.createFromBlock(Block.createString(res));
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_get_attribute(CodeRunner, Value, Value)'

    /**
     * Returns the value of an attribute.
     * @param ctxt context
     * @param element to get attribute from
     * @param ns namespace
     * @param name attribute
     * @return value of given attribute for passed element
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_get_attribute_ns(final CodeRunner ctxt,
                                                             final Value element,
                                                             final Value ns,
                                                             final Value name)
        throws Fail.Exception {
        try {
            final Element e = (Element) element.asBlock().asCustom();
            final String res = e.getAttributeNS(ns.asBlock().asString(),
                                                name.asBlock().asString());
            return Value.createFromBlock(Block.createString(res));
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_get_attribute_ns(CodeRunner, Value, Value, Value)'

    /**
     * Returns the attribute.
     * @param ctxt context
     * @param element to get attribute from
     * @param name attribute
     * @return given attribute for passed element
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_get_attribute_node(final CodeRunner ctxt,
                                                               final Value element,
                                                               final Value name)
        throws Fail.Exception {
        try {
            final Element e = (Element) element.asBlock().asCustom();
            return Cadmium.createObject(e.getAttributeNode(name.asBlock().asString()));
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_get_attribute_node(CodeRunner, Value, Value)'

    /**
     * Returns the attribute.
     * @param ctxt context
     * @param element to get attribute from
     * @param ns namespace
     * @param name attribute
     * @return given attribute for passed element
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_get_attribute_node_ns(final CodeRunner ctxt,
                                                                  final Value element,
                                                                  final Value ns,
                                                                  final Value name)
        throws Fail.Exception {
        try {
            final Element e = (Element) element.asBlock().asCustom();
            return Cadmium.createObject(e.getAttributeNodeNS(ns.asBlock().asString(),
                                                             name.asBlock().asString()));
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_get_attribute_node_ns(CodeRunner, Value, Value, Value)'

    /**
     * Returns child elements having a given tag.
     * @param ctxt context
     * @param element to get children from
     * @param name tag name
     * @return the child elements with given tag name
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_get_elements_by_tag_name(final CodeRunner ctxt,
                                                                     final Value element,
                                                                     final Value name)
        throws Fail.Exception {
        try {
            final Element e = (Element) element.asBlock().asCustom();
            final NodeList l = e.getElementsByTagName(name.asBlock().asString());
            Value res = Value.EMPTY_LIST;
            for (int i = l.getLength() - 1; i >= 0; i--) {
                final Block b = Block.createBlock(Block.TAG_CONS,
                                                  Cadmium.createObject(l.item(i)),
                                                  res);
                res = Value.createFromBlock(b);
            } // end for
            return res;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_get_elements_by_tag_name(CodeRunner, Value, Value)'

    /**
     * Returns child elements having a given tag.
     * @param ctxt context
     * @param element to get children from
     * @param ns namespace
     * @param name tag name
     * @return the child elements with given tag name
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_get_elements_by_tag_name_ns(final CodeRunner ctxt,
                                                                        final Value element,
                                                                        final Value ns,
                                                                        final Value name)
        throws Fail.Exception {
        try {
            final Element e = (Element) element.asBlock().asCustom();
            final NodeList l = e.getElementsByTagNameNS(ns.asBlock().asString(),
                                                        name.asBlock().asString());
            Value res = Value.EMPTY_LIST;
            for (int i = l.getLength() - 1; i >= 0; i--) {
                final Block b = Block.createBlock(Block.TAG_CONS,
                                                  Cadmium.createObject(l.item(i)),
                                                  res);
                res = Value.createFromBlock(b);
            } // end for
            return res;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_get_elements_by_tag_name_ns(CodeRunner, Value, Value, Value)'

    /**
     * Returns the tag name of the passed element.
     * @param ctxt context
     * @param element element
     * @return the tag name of the passed element.
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_get_tag_name(final CodeRunner ctxt,
                                                         final Value element)
        throws Fail.Exception {
        try {
            final Element e = (Element) element.asBlock().asCustom();
            final String res = e.getTagName();
            return Value.createFromBlock(Block.createString(res));
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_get_tag_name(CodeRunner, Value)'

    /**
     * Tests if an element has a given attribute.
     * @param ctxt context
     * @param element element
     * @param name attribute name
     * @return <i>true</i> if the passed element has an attribute of given name,
     *         <i>false</i> otherwise
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_has_attribute(final CodeRunner ctxt,
                                                          final Value element,
                                                          final Value name)
        throws Fail.Exception {
        try {
            final Element e = (Element) element.asBlock().asCustom();
            return e.hasAttribute(name.asBlock().asString()) ? Value.TRUE : Value.FALSE;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_has_attribute(CodeRunner, Value, Value)'

    /**
     * Tests if an element has a given attribute.
     * @param ctxt context
     * @param element element
     * @param ns namespace
     * @param name attribute name
     * @return <i>true</i> if the passed element has an attribute of given name,
     *         <i>false</i> otherwise
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_has_attribute_ns(final CodeRunner ctxt,
                                                             final Value element,
                                                             final Value ns,
                                                             final Value name)
        throws Fail.Exception {
        try {
            final Element e = (Element) element.asBlock().asCustom();
            return e.hasAttributeNS(ns.asBlock().asString(), name.asBlock().asString())
                ? Value.TRUE
                : Value.FALSE;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_has_attribute_ns(CodeRunner, Value, Value, Value)'

    /**
     * Removes an attribute from an element.
     * @param ctxt context
     * @param element element
     * @param name attribute name
     * @return <i>unit</i>
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_remove_attribute(final CodeRunner ctxt,
                                                             final Value element,
                                                             final Value name)
        throws Fail.Exception {
        try {
            final Element e = (Element) element.asBlock().asCustom();
            e.removeAttribute(name.asBlock().asString());
            return Value.UNIT;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_remove_attribute(CodeRunner, Value, Value)'

    /**
     * Removes an attribute from an element.
     * @param ctxt context
     * @param element element
     * @param ns namespace
     * @param name attribute name
     * @return <i>unit</i>
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_remove_attribute_ns(final CodeRunner ctxt,
                                                                final Value element,
                                                                final Value ns,
                                                                final Value name)
        throws Fail.Exception {
        try {
            final Element e = (Element) element.asBlock().asCustom();
            e.removeAttributeNS(ns.asBlock().asString(), name.asBlock().asString());
            return Value.UNIT;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_remove_attribute_ns(CodeRunner, Value, Value, Value)'

    /**
     * Removes an attribute from an element.
     * @param ctxt context
     * @param element element
     * @param attr attribute to remove
     * @return <i>unit</i>
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_remove_attribute_node(final CodeRunner ctxt,
                                                                  final Value element,
                                                                  final Value attr)
        throws Fail.Exception {
        try {
            final Element e = (Element) element.asBlock().asCustom();
            e.removeAttributeNode((Attr) attr.asBlock().asCustom());
            return Value.UNIT;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_remove_attribute_node(CodeRunner, Value, Value)'

    /**
     * Changes the value of an attribute for the passed element.
     * @param ctxt context
     * @param element element
     * @param name attribute name
     * @param value new attribute value
     * @return <i>unit</i>
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_set_attribute(final CodeRunner ctxt,
                                                          final Value element,
                                                          final Value name,
                                                          final Value value)
        throws Fail.Exception {
        try {
            final Element e = (Element) element.asBlock().asCustom();
            e.setAttribute(name.asBlock().asString(), value.asBlock().asString());
            return Value.UNIT;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_set_attribute(CodeRunner, Value, Value, Value)'

    /**
     * Changes the value of an attribute for the passed element.
     * @param ctxt context
     * @param element element
     * @param ns namespace
     * @param name attribute name
     * @param value new attribute value
     * @return <i>unit</i>
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_set_attribute_ns(final CodeRunner ctxt,
                                                             final Value element,
                                                             final Value ns,
                                                             final Value name,
                                                             final Value value)
        throws Fail.Exception {
        try {
            final Element e = (Element) element.asBlock().asCustom();
            e.setAttributeNS(ns.asBlock().asString(),
                             name.asBlock().asString(),
                             value.asBlock().asString());
            return Value.UNIT;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_set_attribute_ns(CodeRunner, Value, Value, Value, Value)'

    /**
     * Returns the parent of the passed entity.
     * @param ctxt context
     * @param entity entity
     * @return the parent of the passed entity
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_get_entity_parent(final CodeRunner ctxt,
                                                              final Value entity)
        throws Fail.Exception {
        try {
            final Entity e = (Entity) entity.asBlock().asCustom();
            return Cadmium.createObject(e.getParentNode());
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_get_entity_parent(CodeRunner, Value)'

    /**
     * Returns the notation name of the passed entity.
     * @param ctxt context
     * @param entity entity
     * @return the notation name of the passed entity
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_get_entity_notation_name(final CodeRunner ctxt,
                                                                     final Value entity)
        throws Fail.Exception {
        try {
            final Entity e = (Entity) entity.asBlock().asCustom();
            final String res = e.getNotationName();
            return Value.createFromBlock(Block.createString(res));
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_get_entity_notation_name(CodeRunner, Value)'

    /**
     * Returns the public ID of the passed entity.
     * @param ctxt context
     * @param entity entity
     * @return the public ID of the passed entity
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_get_entity_public_id(final CodeRunner ctxt,
                                                                 final Value entity)
        throws Fail.Exception {
        try {
            final Entity e = (Entity) entity.asBlock().asCustom();
            final String res = e.getPublicId();
            return Value.createFromBlock(Block.createString(res));
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_get_entity_public_id(CodeRunner, Value)'

    /**
     * Returns the system ID of the passed entity.
     * @param ctxt context
     * @param entity entity
     * @return the system ID of the passed entity
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_get_entity_system_id(final CodeRunner ctxt,
                                                                 final Value entity)
        throws Fail.Exception {
        try {
            final Entity e = (Entity) entity.asBlock().asCustom();
            final String res = e.getSystemId();
            return Value.createFromBlock(Block.createString(res));
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_get_entity_system_id(CodeRunner, Value)'

    /**
     * Returns the parent of the passed notation.
     * @param ctxt context
     * @param notation notation
     * @return the parent of the passed notation
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_get_notation_parent(final CodeRunner ctxt,
                                                                final Value notation)
        throws Fail.Exception {
        try {
            final Notation n = (Notation) notation.asBlock().asCustom();
            return Cadmium.createObject(n.getParentNode());
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_get_notation_parent(CodeRunner, Value)'

    /**
     * Returns the public ID of the passed notation.
     * @param ctxt context
     * @param notation notation
     * @return the public ID of the passed notation
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_get_notation_public_id(final CodeRunner ctxt,
                                                                   final Value notation)
        throws Fail.Exception {
        try {
            final Notation n = (Notation) notation.asBlock().asCustom();
            final String res = n.getPublicId();
            return Value.createFromBlock(Block.createString(res));
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_get_notation_public_id(CodeRunner, Value)'

    /**
     * Returns the system ID of the passed notation.
     * @param ctxt context
     * @param notation notation
     * @return the system ID of the passed notation
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_get_notation_system_id(final CodeRunner ctxt,
                                                                   final Value notation)
        throws Fail.Exception {
        try {
            final Notation n = (Notation) notation.asBlock().asCustom();
            final String res = n.getSystemId();
            return Value.createFromBlock(Block.createString(res));
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_get_notation_system_id(CodeRunner, Value)'

    /**
     * Returns the parent of the passed processing instruction.
     * @param ctxt context
     * @param pi processing instruction
     * @return the parent of the passed processing instruction
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_get_processing_instruction_parent(final CodeRunner ctxt,
                                                                              final Value pi)
        throws Fail.Exception {
        try {
            final ProcessingInstruction p =
                (ProcessingInstruction) pi.asBlock().asCustom();
            return Cadmium.createObject(p.getParentNode());
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_get_processing_instruction_parent(CodeRunner, Value)'

    /**
     * Returns the target of the passed processing instruction.
     * @param ctxt context
     * @param pi processing instruction
     * @return the target of the passed processing instruction
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_get_processing_instruction_target(final CodeRunner ctxt,
                                                                              final Value pi)
        throws Fail.Exception {
        try {
            final ProcessingInstruction p =
                (ProcessingInstruction) pi.asBlock().asCustom();
            final String res = p.getTarget();
            return Value.createFromBlock(Block.createString(res));
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_get_processing_instruction_target(CodeRunner, Value)'

    /**
     * Returns the data of the passed processing instruction.
     * @param ctxt context
     * @param pi processing instruction
     * @return the data of the passed processing instruction
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_get_processing_instruction_data(final CodeRunner ctxt,
                                                                            final Value pi)
        throws Fail.Exception {
        try {
            final ProcessingInstruction p =
                (ProcessingInstruction) pi.asBlock().asCustom();
            final String res = p.getData();
            return Value.createFromBlock(Block.createString(res));
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_get_processing_instruction_data(CodeRunner, Value)'

    /**
     * Changes the data of the passed processing instruction.
     * @param ctxt context
     * @param pi processing instruction
     * @param value new data
     * @return <i>unit</i>
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_set_processing_instruction_data(final CodeRunner ctxt,
                                                                            final Value pi,
                                                                            final Value value)
        throws Fail.Exception {
        try {
            final ProcessingInstruction p =
                (ProcessingInstruction) pi.asBlock().asCustom();
            p.setData(value.asBlock().asString());
            return Value.UNIT;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_set_processing_instruction_data(CodeRunner, Value, Value)'

    /**
     * Returns the parent of the passed text.
     * @param ctxt context
     * @param text text
     * @return the parent of the passed text
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_get_text_parent(final CodeRunner ctxt,
                                                            final Value text)
        throws Fail.Exception {
        try {
            final Text t = (Text) text.asBlock().asCustom();
            return Cadmium.createObject(t.getParentNode());
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_get_text_parent(CodeRunner, Value)'

    /**
     * Returns the data of the passed texte.
     * @param ctxt context
     * @param text text
     * @return the data of the passed text
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_get_text(final CodeRunner ctxt,
                                                     final Value text)
        throws Fail.Exception {
        try {
            final Text t = (Text) text.asBlock().asCustom();
            final String res = t.getData();
            return Value.createFromBlock(Block.createString(res));
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_get_text(CodeRunner, Value)'

    /**
     * Changes the data of the passed text.
     * @param ctxt context
     * @param text text
     * @param value new data
     * @return <i>unit</i>
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_set_text(final CodeRunner ctxt,
                                                     final Value text,
                                                     final Value value)
        throws Fail.Exception {
        try {
            final Text t = (Text) text.asBlock().asCustom();
            t.setData(value.asBlock().asString());
            return Value.UNIT;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_set_text(CodeRunner, Value, Value)'

    /**
     * Returns the parent of the passed node.
     * @param ctxt context
     * @param node node
     * @return the parent of the passed node
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_get_parent(final CodeRunner ctxt,
                                                       final Value node)
        throws Fail.Exception {
        try {
            final Node n = (Node) node.asBlock().get(0).asBlock().asCustom();
            final Node res = n.getParentNode();
            if (res != null) {
                return createNode(res);
            } else {
                Fail.raiseNotFound();
                return Value.UNIT; // never reached
            } // end if/else
        } catch (final Fail.Exception fe) {
            throw fe;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_get_parent(CodeRunner, Value)'

    /**
     * Returns the enclosing document of the passed node.
     * @param ctxt context
     * @param node node
     * @return the enclosing document of the passed node
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_get_document(final CodeRunner ctxt,
                                                         final Value node)
        throws Fail.Exception {
        try {
            final Node n = (Node) node.asBlock().get(0).asBlock().asCustom();
            return Cadmium.createObject(n.getOwnerDocument());
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_get_document(CodeRunner, Value)'

    /**
     * Adds an attribute child to the passed node.
     * @param ctxt context
     * @param node node
     * @param value attribute name
     * @return the added child
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_append_attr_child(final CodeRunner ctxt,
                                                              final Value node,
                                                              final Value value)
        throws Fail.Exception {
        try {
            final Node n = (Node) node.asBlock().get(0).asBlock().asCustom();
            final Document d = n.getOwnerDocument();
            final Node c = d.createAttribute(value.asBlock().asString());
            n.appendChild(c);
            return Cadmium.createObject(c);
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_append_attr_child(CodeRunner, Value, Value)'

    /**
     * Adds an attribute child to the passed node.
     * @param ctxt context
     * @param node node
     * @param ns namespace
     * @param value attribute name
     * @return the added child
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_append_attr_ns_child(final CodeRunner ctxt,
                                                                 final Value node,
                                                                 final Value ns,
                                                                 final Value value)
        throws Fail.Exception {
        try {
            final Node n = (Node) node.asBlock().get(0).asBlock().asCustom();
            final Document d = n.getOwnerDocument();
            final Node c = d.createAttributeNS(ns.asBlock().asString(),
                                               value.asBlock().asString());
            n.appendChild(c);
            return Cadmium.createObject(c);
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_append_attr_ns_child(CodeRunner, Value, Value, Value)'

    /**
     * Adds a cdata child to the passed node.
     * @param ctxt context
     * @param node node
     * @param value data
     * @return the added child
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_append_cdata_child(final CodeRunner ctxt,
                                                               final Value node,
                                                               final Value value)
        throws Fail.Exception {
        try {
            final Node n = (Node) node.asBlock().get(0).asBlock().asCustom();
            final Document d = n.getOwnerDocument();
            final Node c = d.createCDATASection(value.asBlock().asString());
            n.appendChild(c);
            return Cadmium.createObject(c);
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_append_cdata_child(CodeRunner, Value, Value)'

    /**
     * Adds a comment child to the passed node.
     * @param ctxt context
     * @param node node
     * @param value data
     * @return the added child
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_append_comment_child(final CodeRunner ctxt,
                                                                 final Value node,
                                                                 final Value value)
        throws Fail.Exception {
        try {
            final Node n = (Node) node.asBlock().get(0).asBlock().asCustom();
            final Document d = n.getOwnerDocument();
            final Node c = d.createComment(value.asBlock().asString());
            n.appendChild(c);
            return Cadmium.createObject(c);
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_append_comment_child(CodeRunner, Value, Value)'

    /**
     * Adds an element child to the passed node.
     * @param ctxt context
     * @param node node
     * @param value element tag
     * @return the added child
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_append_element_child(final CodeRunner ctxt,
                                                                 final Value node,
                                                                 final Value value)
        throws Fail.Exception {
        try {
            final Node n = (Node) node.asBlock().get(0).asBlock().asCustom();
            final Document d = n.getOwnerDocument();
            final Node c = d.createElement(value.asBlock().asString());
            n.appendChild(c);
            return Cadmium.createObject(c);
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_append_element_child(CodeRunner, Value, Value)'

    /**
     * Adds an element child to the passed node.
     * @param ctxt context
     * @param node node
     * @param ns namespace
     * @param value element tag
     * @return the added child
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_append_element_ns_child(final CodeRunner ctxt,
                                                                    final Value node,
                                                                    final Value ns,
                                                                    final Value value)
        throws Fail.Exception {
        try {
            final Node n = (Node) node.asBlock().get(0).asBlock().asCustom();
            final Document d = n.getOwnerDocument();
            final Node c = d.createElementNS(ns.asBlock().asString(),
                                             value.asBlock().asString());
            n.appendChild(c);
            return Cadmium.createObject(c);
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_append_element_ns_child(CodeRunner, Value, Value, Value)'

    /**
     * Adds an entity reference child to the passed node.
     * @param ctxt context
     * @param node node
     * @param value reference name
     * @return the added child
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_append_entity_reference_child(final CodeRunner ctxt,
                                                                          final Value node,
                                                                          final Value value)
        throws Fail.Exception {
        try {
            final Node n = (Node) node.asBlock().get(0).asBlock().asCustom();
            final Document d = n.getOwnerDocument();
            final Node c = d.createEntityReference(value.asBlock().asString());
            n.appendChild(c);
            return Cadmium.createObject(c);
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_append_entity_reference_child(CodeRunner, Value, Value)'

    /**
     * Adds an attribute child to the passed node.
     * @param ctxt context
     * @param node node
     * @param before child to insert before
     * @param value attribute name
     * @return the added child
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_insert_attr_child_before(final CodeRunner ctxt,
                                                                     final Value node,
                                                                     final Value before,
                                                                     final Value value)
        throws Fail.Exception {
        try {
            final Node n = (Node) node.asBlock().get(0).asBlock().asCustom();
            final Node b = (Node) before.asBlock().get(0).asBlock().asCustom();
            final Document d = n.getOwnerDocument();
            final Node c = d.createAttribute(value.asBlock().asString());
            n.insertBefore(c, b);
            return Cadmium.createObject(c);
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_insert_attr_child_before(CodeRunner, Value, Value, Value)'

    /**
     * Adds an attribute child to the passed node.
     * @param ctxt context
     * @param node node
     * @param before child to insert before
     * @param ns namespace
     * @param value attribute name
     * @return the added child
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_insert_attr_ns_child_before(final CodeRunner ctxt,
                                                                        final Value node,
                                                                        final Value before,
                                                                        final Value ns,
                                                                        final Value value)
        throws Fail.Exception {
        try {
            final Node n = (Node) node.asBlock().get(0).asBlock().asCustom();
            final Node b = (Node) before.asBlock().get(0).asBlock().asCustom();
            final Document d = n.getOwnerDocument();
            final Node c = d.createAttributeNS(ns.asBlock().asString(),
                                               value.asBlock().asString());
            n.insertBefore(c, b);
            return Cadmium.createObject(c);
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_insert_attr_ns_child_before(CodeRunner, Value, Value, Value, Value)'

    /**
     * Adds a cdata child to the passed node.
     * @param ctxt context
     * @param node node
     * @param before child to insert before
     * @param value data
     * @return the added child
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_insert_cdata_child_before(final CodeRunner ctxt,
                                                                      final Value node,
                                                                      final Value before,
                                                                      final Value value)
        throws Fail.Exception {
        try {
            final Node n = (Node) node.asBlock().get(0).asBlock().asCustom();
            final Node b = (Node) before.asBlock().get(0).asBlock().asCustom();
            final Document d = n.getOwnerDocument();
            final Node c = d.createCDATASection(value.asBlock().asString());
            n.insertBefore(c, b);
            return Cadmium.createObject(c);
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_insert_cdata_child_before(CodeRunner, Value, Value, Value)'

    /**
     * Adds a comment child to the passed node.
     * @param ctxt context
     * @param node node
     * @param before child to insert before
     * @param value data
     * @return the added child
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_insert_comment_child_before(final CodeRunner ctxt,
                                                                        final Value node,
                                                                        final Value before,
                                                                        final Value value)
        throws Fail.Exception {
        try {
            final Node n = (Node) node.asBlock().get(0).asBlock().asCustom();
            final Node b = (Node) before.asBlock().get(0).asBlock().asCustom();
            final Document d = n.getOwnerDocument();
            final Node c = d.createComment(value.asBlock().asString());
            n.insertBefore(c, b);
            return Cadmium.createObject(c);
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_insert_comment_child_before(CodeRunner, Value, Value, Value)'

    /**
     * Adds an element child to the passed node.
     * @param ctxt context
     * @param node node
     * @param before child to insert before
     * @param value element tag
     * @return the added child
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_insert_element_child_before(final CodeRunner ctxt,
                                                                        final Value node,
                                                                        final Value before,
                                                                        final Value value)
        throws Fail.Exception {
        try {
            final Node n = (Node) node.asBlock().get(0).asBlock().asCustom();
            final Node b = (Node) before.asBlock().get(0).asBlock().asCustom();
            final Document d = n.getOwnerDocument();
            final Node c = d.createElement(value.asBlock().asString());
            n.insertBefore(c, b);
            return Cadmium.createObject(c);
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_insert_element_child_before(CodeRunner, Value, Value, Value)'

    /**
     * Adds an element child to the passed node.
     * @param ctxt context
     * @param node node
     * @param before child to insert before
     * @param ns namespace
     * @param value element tag
     * @return the added child
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_insert_element_ns_child_before(final CodeRunner ctxt,
                                                                           final Value node,
                                                                           final Value before,
                                                                           final Value ns,
                                                                           final Value value)
        throws Fail.Exception {
        try {
            final Node n = (Node) node.asBlock().get(0).asBlock().asCustom();
            final Node b = (Node) before.asBlock().get(0).asBlock().asCustom();
            final Document d = n.getOwnerDocument();
            final Node c = d.createElementNS(ns.asBlock().asString(),
                                             value.asBlock().asString());
            n.insertBefore(c, b);
            return Cadmium.createObject(c);
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_insert_element_ns_child_before(CodeRunner, Value, Value, Value, Value)'

    /**
     * Adds an entity reference child to the passed node.
     * @param ctxt context
     * @param node node
     * @param before child to insert before
     * @param value reference name
     * @return the added child
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_insert_entity_reference_child_before(final CodeRunner ctxt,
                                                                                 final Value node,
                                                                                 final Value before,
                                                                                 final Value value)
        throws Fail.Exception {
        try {
            final Node n = (Node) node.asBlock().get(0).asBlock().asCustom();
            final Node b = (Node) before.asBlock().get(0).asBlock().asCustom();
            final Document d = n.getOwnerDocument();
            final Node c = d.createEntityReference(value.asBlock().asString());
            n.insertBefore(c, b);
            return Cadmium.createObject(c);
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_insert_entity_reference_child_before(CodeRunner, Value, Value, Value)'

    /**
     * Returns the child nodes of the passed node.
     * @param ctxt context
     * @param node node
     * @return the child nodes of the passed node
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_get_child_nodes(final CodeRunner ctxt,
                                                            final Value node)
        throws Fail.Exception {
        try {
            final Node n = (Node) node.asBlock().get(0).asBlock().asCustom();
            final NodeList l = n.getChildNodes();
            Value res = Value.EMPTY_LIST;
            for (int i = l.getLength() - 1; i >= 0; i--) {
                final Block b = Block.createBlock(Block.TAG_CONS,
                                                  createNode(l.item(i)),
                                                  res);
                res = Value.createFromBlock(b);
            } // end for
            return res;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_get_child_nodes(CodeRunner, Value)'

    /**
     * Returns the first child of the passed node.
     * @param ctxt context
     * @param node node
     * @return the first child of the passed node
     * @throws Fail.Exception raise <i>Not_found</i> if such a node does not exit
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_get_first_child(final CodeRunner ctxt,
                                                            final Value node)
        throws Fail.Exception {
        try {
            final Node n = (Node) node.asBlock().get(0).asBlock().asCustom();
            final Node res = n.getFirstChild();
            if (res != null) {
                return createNode(res);
            } else {
                Fail.raiseNotFound();
                return Value.UNIT; // never reached
            } // end if/else
        } catch (final Fail.Exception fe) {
            throw fe;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_get_first_child(CodeRunner, Value)'

    /**
     * Returns the last child of the passed node.
     * @param ctxt context
     * @param node node
     * @return the last child of the passed node
     * @throws Fail.Exception raise <i>Not_found</i> if such a node does not exit
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_get_last_child(final CodeRunner ctxt,
                                                           final Value node)
        throws Fail.Exception {
        try {
            final Node n = (Node) node.asBlock().get(0).asBlock().asCustom();
            final Node res = n.getLastChild();
            if (res != null) {
                return createNode(res);
            } else {
                Fail.raiseNotFound();
                return Value.UNIT; // never reached
            } // end if/else
        } catch (final Fail.Exception fe) {
            throw fe;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_get_last_child(CodeRunner, Value)'

    /**
     * Returns the previous sibling of the passed node.
     * @param ctxt context
     * @param node node
     * @return the previous sibling of the passed node
     * @throws Fail.Exception raise <i>Not_found</i> if such a node does not exit
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_get_previous_sibling(final CodeRunner ctxt,
                                                                 final Value node)
        throws Fail.Exception {
        try {
            final Node n = (Node) node.asBlock().get(0).asBlock().asCustom();
            final Node res = n.getPreviousSibling();
            if (res != null) {
                return createNode(res);
            } else {
                Fail.raiseNotFound();
                return Value.UNIT; // never reached
            } // end if/else
        } catch (final Fail.Exception fe) {
            throw fe;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_get_previous_sibling(CodeRunner, Value)'

    /**
     * Returns the next sibling of the passed node.
     * @param ctxt context
     * @param node node
     * @return the next sibling of the passed node
     * @throws Fail.Exception raise <i>Not_found</i> if such a node does not exit
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_get_next_sibling(final CodeRunner ctxt,
                                                             final Value node)
        throws Fail.Exception {
        try {
            final Node n = (Node) node.asBlock().get(0).asBlock().asCustom();
            final Node res = n.getNextSibling();
            if (res != null) {
                return createNode(res);
            } else {
                Fail.raiseNotFound();
                return Value.UNIT; // never reached
            } // end if/else
        } catch (final Fail.Exception fe) {
            throw fe;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_get_next_sibling(CodeRunner, Value)'

    /**
     * Removes a child of the passed node.
     * @param ctxt context
     * @param node node
     * @param child child to remove
     * @return <i>unit</i>
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_remove_child(final CodeRunner ctxt,
                                                         final Value node,
                                                         final Value child)
        throws Fail.Exception {
        try {
            final Node n = (Node) node.asBlock().get(0).asBlock().asCustom();
            final Node c = (Node) child.asBlock().get(0).asBlock().asCustom();
            n.removeChild(c);
            return Value.UNIT;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_remove_child(CodeRunner, Value, Value)'

    /**
     * Replaces a node by another one.
     * @param ctxt context
     * @param node node
     * @param newChild new child
     * @param oldChild old child
     * @return <i>unit</i>
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_document_replace_child(final CodeRunner ctxt,
                                                          final Value node,
                                                          final Value newChild,
                                                          final Value oldChild)
        throws Fail.Exception {
        try {
            final Node n = (Node) node.asBlock().get(0).asBlock().asCustom();
            final Node nc = (Node) newChild.asBlock().get(0).asBlock().asCustom();
            final Node oc = (Node) oldChild.asBlock().get(0).asBlock().asCustom();
            n.replaceChild(nc, oc);
            return Value.UNIT;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_document_replace_child(CodeRunner, Value, Value, Value)'

    /**
     * Constructs a factory.
     * @param ctxt context
     * @param unit ignored
     * @return a new factory
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_documentfactory_make(final CodeRunner ctxt,
                                                        final Value unit)
        throws Fail.Exception {
        try {
            return Cadmium.createObject(DocumentBuilderFactory.newInstance());
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_documentfactory_make(CodeRunner, Value)'

    /**
     * Sets the value of an attribute.
     * @param ctxt context
     * @param fact factory
     * @param name attribute name
     * @param value attribute value
     * @return <i>unit</i>
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_documentfactory_set_attribute(final CodeRunner ctxt,
                                                                 final Value fact,
                                                                 final Value name,
                                                                 final Value value)
        throws Fail.Exception {
        try {
            final DocumentBuilderFactory f =
                (DocumentBuilderFactory) fact.asBlock().asCustom();
            f.setAttribute(name.asBlock().asString(), value.asBlock().asCustom());
            return Value.UNIT;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_documentfactory_set_attribute(CodeRunner, Value, Value, Value)'

    /**
     * Gets the value of an attribute.
     * @param ctxt context
     * @param fact factory
     * @param name attribute name
     * @return value of passed attribute
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_documentfactory_get_attribute(final CodeRunner ctxt,
                                                                 final Value fact,
                                                                 final Value name)
        throws Fail.Exception {
        try {
            final DocumentBuilderFactory f =
                (DocumentBuilderFactory) fact.asBlock().asCustom();
            return Cadmium.createObject(f.getAttribute(name.asBlock().asString()));
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_documentfactory_get_attribute(CodeRunner, Value, Value)'

    /**
     * Sets the value of a feature flag.
     * @param ctxt context
     * @param fact factory
     * @param name feature name
     * @param value feature value
     * @return <i>unit</i>
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_documentfactory_set_feature(final CodeRunner ctxt,
                                                               final Value fact,
                                                               final Value name,
                                                               final Value value)
        throws Fail.Exception {
        try {
            final DocumentBuilderFactory f =
                (DocumentBuilderFactory) fact.asBlock().asCustom();
            f.setFeature(name.asBlock().asString(), value == Value.TRUE);
            return Value.UNIT;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_documentfactory_set_feature(CodeRunner, Value, Value, Value)'

    /**
     * Gets the value of a feature flag.
     * @param ctxt context
     * @param fact factory
     * @param name feature name
     * @return value of passed feature
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_documentfactory_get_feature(final CodeRunner ctxt,
                                                               final Value fact,
                                                               final Value name)
        throws Fail.Exception {
        try {
            final DocumentBuilderFactory f =
                (DocumentBuilderFactory) fact.asBlock().asCustom();
            return f.getFeature(name.asBlock().asString())
                ? Value.TRUE
                : Value.FALSE;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_documentfactory_get_feature(CodeRunner, Value, Value)'

    /**
     * Sets the schema for constructed DOM parsers.
     * @param ctxt context
     * @param fact factory
     * @param schema schema to use
     * @return <i>unit</i>
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_documentfactory_set_schema(final CodeRunner ctxt,
                                                              final Value fact,
                                                              final Value schema)
        throws Fail.Exception {
        try {
            final DocumentBuilderFactory f =
                (DocumentBuilderFactory) fact.asBlock().asCustom();
            f.setSchema((Schema) schema.asBlock().asCustom());
            return Value.UNIT;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_documentfactory_set_schema(CodeRunner, Value, Value)'

    /**
     * Gets the schema for constructed DOM parsers.
     * @param ctxt context
     * @param fact factory
     * @return schema used by constructed parsers
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_documentfactory_get_schema(final CodeRunner ctxt,
                                                              final Value fact)
        throws Fail.Exception {
        try {
            final DocumentBuilderFactory f =
                (DocumentBuilderFactory) fact.asBlock().asCustom();
            return Cadmium.createObject(f.getSchema());
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_documentfactory_get_schema(CodeRunner, Value)'

    /**
     * Sets the 'coalescing' property for constructed DOM parsers.
     * @param ctxt context
     * @param fact factory
     * @param value value for 'coalescing' property
     * @return <i>unit</i>
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_documentfactory_set_coalescing(final CodeRunner ctxt,
                                                                  final Value fact,
                                                                  final Value value)
        throws Fail.Exception {
        try {
            final DocumentBuilderFactory f =
                (DocumentBuilderFactory) fact.asBlock().asCustom();
            f.setCoalescing(value == Value.TRUE);
            return Value.UNIT;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_documentfactory_set_coalescing(CodeRunner, Value, Value)'

    /**
     * Gets the 'coalescing' property for constructed DOM parsers.
     * @param ctxt context
     * @param fact factory
     * @return value for 'coalesing' property
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_documentfactory_is_coalescing(final CodeRunner ctxt,
                                                                 final Value fact)
        throws Fail.Exception {
        try {
            final DocumentBuilderFactory f =
                (DocumentBuilderFactory) fact.asBlock().asCustom();
            return f.isCoalescing() ? Value.TRUE : Value.FALSE;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_documentfactory_is_coalescing(CodeRunner, Value)'

    /**
     * Sets the 'expand-entity-references' property for constructed DOM parsers.
     * @param ctxt context
     * @param fact factory
     * @param value value for 'expand-entity-references' property
     * @return <i>unit</i>
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_documentfactory_set_expand_entity_references(final CodeRunner ctxt,
                                                                                final Value fact,
                                                                                final Value value)
        throws Fail.Exception {
        try {
            final DocumentBuilderFactory f =
                (DocumentBuilderFactory) fact.asBlock().asCustom();
            f.setExpandEntityReferences(value == Value.TRUE);
            return Value.UNIT;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_documentfactory_set_expand_entity_references(CodeRunner, Value, Value)'

    /**
     * Gets the 'expand-entity-reference' property for constructed DOM parsers.
     * @param ctxt context
     * @param fact factory
     * @return value for 'expand-entity-references' property
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_documentfactory_is_expand_entity_references(final CodeRunner ctxt,
                                                                               final Value fact)
        throws Fail.Exception {
        try {
            final DocumentBuilderFactory f =
                (DocumentBuilderFactory) fact.asBlock().asCustom();
            return f.isExpandEntityReferences() ? Value.TRUE : Value.FALSE;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_documentfactory_is_expand_entity_references(CodeRunner, Value)'

    /**
     * Sets the 'ignoring-comments' property for constructed DOM parsers.
     * @param ctxt context
     * @param fact factory
     * @param value value for 'ignoring-comments' property
     * @return <i>unit</i>
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_documentfactory_set_ignoring_comments(final CodeRunner ctxt,
                                                                         final Value fact,
                                                                         final Value value)
        throws Fail.Exception {
        try {
            final DocumentBuilderFactory f =
                (DocumentBuilderFactory) fact.asBlock().asCustom();
            f.setIgnoringComments(value == Value.TRUE);
            return Value.UNIT;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_documentfactory_set_ignoring_comments(CodeRunner, Value, Value)'

    /**
     * Gets the 'ignoring-comments' property for constructed DOM parsers.
     * @param ctxt context
     * @param fact factory
     * @return value for 'ignoring-comments' property
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_documentfactory_is_ignoring_comments(final CodeRunner ctxt,
                                                                        final Value fact)
        throws Fail.Exception {
        try {
            final DocumentBuilderFactory f =
                (DocumentBuilderFactory) fact.asBlock().asCustom();
            return f.isIgnoringComments() ? Value.TRUE : Value.FALSE;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_documentfactory_is_ignoring_comments(CodeRunner, Value)'

    /**
     * Sets the 'ignoring-element-content-whitespace' property for constructed DOM parsers.
     * @param ctxt context
     * @param fact factory
     * @param value value for 'ignoring-element-content-whitespace' property
     * @return <i>unit</i>
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_documentfactory_set_ignoring_element_content_whitespace(final CodeRunner ctxt,
                                                                                           final Value fact,
                                                                                           final Value value)
        throws Fail.Exception {
        try {
            final DocumentBuilderFactory f =
                (DocumentBuilderFactory) fact.asBlock().asCustom();
            f.setIgnoringElementContentWhitespace(value == Value.TRUE);
            return Value.UNIT;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_documentfactory_set_ignoring_element_content_whitespace(CodeRunner, Value, Value)'

    /**
     * Gets the 'ignoring-element-conent-whitespace' property for constructed DOM parsers.
     * @param ctxt context
     * @param fact factory
     * @return value for 'ignoring-element-content-whitespace' property
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_documentfactory_is_ignoring_element_content_whitespace(final CodeRunner ctxt,
                                                                                          final Value fact)
        throws Fail.Exception {
        try {
            final DocumentBuilderFactory f =
                (DocumentBuilderFactory) fact.asBlock().asCustom();
            return f.isIgnoringElementContentWhitespace() ? Value.TRUE : Value.FALSE;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_documentfactory_is_ignoring_element_content_whitespace(CodeRunner, Value)'

    /**
     * Sets the 'namespace-aware' property for constructed DOM parsers.
     * @param ctxt context
     * @param fact factory
     * @param value value for 'namespace-aware' property
     * @return <i>unit</i>
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_documentfactory_set_namespace_aware(final CodeRunner ctxt,
                                                                       final Value fact,
                                                                       final Value value)
        throws Fail.Exception {
        try {
            final DocumentBuilderFactory f =
                (DocumentBuilderFactory) fact.asBlock().asCustom();
            f.setNamespaceAware(value == Value.TRUE);
            return Value.UNIT;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_documentfactory_set_namespace_aware(CodeRunner, Value, Value)'

    /**
     * Gets the 'namespace-aware' property for constructed DOM parsers.
     * @param ctxt context
     * @param fact factory
     * @return value for 'namespace-aware' property
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_documentfactory_is_namespace_aware(final CodeRunner ctxt,
                                                                      final Value fact)
        throws Fail.Exception {
        try {
            final DocumentBuilderFactory f =
                (DocumentBuilderFactory) fact.asBlock().asCustom();
            return f.isNamespaceAware() ? Value.TRUE : Value.FALSE;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_documentfactory_is_namespace_aware(CodeRunner, Value)'

    /**
     * Sets the 'validating' property for constructed DOM parsers.
     * @param ctxt context
     * @param fact factory
     * @param value value for 'validating' property
     * @return <i>unit</i>
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_documentfactory_set_validating(final CodeRunner ctxt,
                                                                  final Value fact,
                                                                  final Value value)
        throws Fail.Exception {
        try {
            final DocumentBuilderFactory f =
                (DocumentBuilderFactory) fact.asBlock().asCustom();
            f.setValidating(value == Value.TRUE);
            return Value.UNIT;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_documentfactory_set_validating(CodeRunner, Value, Value)'

    /**
     * Gets the 'validating' property for constructed DOM parsers.
     * @param ctxt context
     * @param fact factory
     * @return value for 'validating' property
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_documentfactory_is_validating(final CodeRunner ctxt,
                                                                 final Value fact)
        throws Fail.Exception {
        try {
            final DocumentBuilderFactory f =
                (DocumentBuilderFactory) fact.asBlock().asCustom();
            return f.isValidating() ? Value.TRUE : Value.FALSE;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_documentfactory_is_validating(CodeRunner, Value)'

    /**
     * Sets the 'xinclude-aware' property for constructed DOM parsers.
     * @param ctxt context
     * @param fact factory
     * @param value value for 'xinclude-aware' property
     * @return <i>unit</i>
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_documentfactory_set_xinclude_aware(final CodeRunner ctxt,
                                                                      final Value fact,
                                                                      final Value value)
        throws Fail.Exception {
        try {
            final DocumentBuilderFactory f =
                (DocumentBuilderFactory) fact.asBlock().asCustom();
            f.setXIncludeAware(value == Value.TRUE);
            return Value.UNIT;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_documentfactory_set_xinclude_aware(CodeRunner, Value, Value)'

    /**
     * Gets the 'xinclude-aware' property for constructed DOM parsers.
     * @param ctxt context
     * @param fact factory
     * @return value for 'xinclude-aware' property
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_documentfactory_is_xinclude_aware(final CodeRunner ctxt,
                                                                     final Value fact)
        throws Fail.Exception {
        try {
            final DocumentBuilderFactory f =
                (DocumentBuilderFactory) fact.asBlock().asCustom();
            return f.isXIncludeAware() ? Value.TRUE : Value.FALSE;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_documentfactory_is_xinclude_aware(CodeRunner, Value)'

    /**
     * Constructs a builder from a factory.
     * @param ctxt context
     * @param fact factory
     * @return a new builder
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_documentbuilder_make(final CodeRunner ctxt,
                                                        final Value fact)
        throws Fail.Exception {
        try {
            final DocumentBuilderFactory f =
                (DocumentBuilderFactory) fact.asBlock().asCustom();
            return Cadmium.createObject(f.newDocumentBuilder());
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_documentbuilder_make(CodeRunner, Value)'

    /**
     * Gets the schema for passed builder
     * @param ctxt context
     * @param builder builder
     * @return schema used by passed builder
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_documentbuilder_get_schema(final CodeRunner ctxt,
                                                              final Value builder)
        throws Fail.Exception {
        try {
            final DocumentBuilder b = (DocumentBuilder) builder.asBlock().asCustom();
            return Cadmium.createObject(b.getSchema());
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_documentbuilder_get_schema(CodeRunner, Value)'

    /**
     * Gets the 'namespace-aware' property for passed builder.
     * @param ctxt context
     * @param builder builder
     * @return value for 'namespace-aware' property
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_documentbuilder_is_namespace_aware(final CodeRunner ctxt,
                                                                      final Value builder)
        throws Fail.Exception {
        try {
            final DocumentBuilder b = (DocumentBuilder) builder.asBlock().asCustom();
            return b.isNamespaceAware() ? Value.TRUE : Value.FALSE;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_documentbuilder_is_namespace_aware(CodeRunner, Value)'

    /**
     * Gets the 'validating' property for passed builder.
     * @param ctxt context
     * @param builder builder
     * @return value for 'validating' property
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_documentbuilder_is_validating(final CodeRunner ctxt,
                                                                 final Value builder)
        throws Fail.Exception {
        try {
            final DocumentBuilder b = (DocumentBuilder) builder.asBlock().asCustom();
            return b.isValidating() ? Value.TRUE : Value.FALSE;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_documentbuilder_is_validating(CodeRunner, Value)'

    /**
     * Gets the 'xinclude-aware' property for passed builder.
     * @param ctxt context
     * @param builder builder
     * @return value for 'xinclude-aware' property
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_documentbuilder_is_xinclude_aware(final CodeRunner ctxt,
                                                                     final Value builder)
        throws Fail.Exception {
        try {
            final DocumentBuilder b = (DocumentBuilder) builder.asBlock().asCustom();
            return b.isXIncludeAware() ? Value.TRUE : Value.FALSE;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_documentbuilder_is_xinclude_aware(CodeRunner, Value)'

    /**
     * Creates an empty document.
     * @param ctxt context
     * @param builder builder
     * @return empty document
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_documentbuilder_new_document(final CodeRunner ctxt,
                                                                final Value builder)
        throws Fail.Exception {
        try {
            final DocumentBuilder b = (DocumentBuilder) builder.asBlock().asCustom();
            return Cadmium.createObject(b.newDocument());
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_documentbuilder_new_document(CodeRunner, Value)'

    /**
     * Parses a file using a document builder.
     * @param ctxt context
     * @param builder builder
     * @param file file to parse
     * @return parsed document
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_documentbuilder_parse_file(final CodeRunner ctxt,
                                                              final Value builder,
                                                              final Value file)
        throws Fail.Exception {
        try {
            final DocumentBuilder b = (DocumentBuilder) builder.asBlock().asCustom();
            return Cadmium.createObject(b.parse(ctxt.getContext().getInputStreamForPath(file)));
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_documentbuilder_parse_file(CodeRunner, Value, Value)'

    /**
     * Parses a channel using a document builder.
     * @param ctxt context
     * @param builder builder
     * @param channel channel to parse
     * @return parsed document
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_documentbuilder_parse_channel(final CodeRunner ctxt,
                                                                 final Value builder,
                                                                 final Value channel)
        throws Fail.Exception {
        try {
            final DocumentBuilder b = (DocumentBuilder) builder.asBlock().asCustom();
            final Channel ch = (Channel) channel.asBlock().asCustom();
            return Cadmium.createObject(b.parse(ch.asInputStream()));
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_documentbuilder_parse_channel(CodeRunner, Value, Value)'

    /**
     * Resets the passed builder.
     * @param ctxt context
     * @param builder builder
     * @return <i>unit</i>
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_documentbuilder_reset(final CodeRunner ctxt,
                                                         final Value builder)
        throws Fail.Exception {
        try {
            final DocumentBuilder b = (DocumentBuilder) builder.asBlock().asCustom();
            b.reset();
            return Value.UNIT;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_documentbuilder_reset(CodeRunner, Value)'

    /**
     * Encodes an Objective Caml node instance from a Java node instance.
     * @param n node to encode - should not be <tt>null</tt>
     * @return encoded node
     * @throws Fail.Exception if the passed node has an unknown type
     */
    private static Value createNode(final Node n) throws Fail.Exception {
        assert n != null : "null n";
        if (n instanceof Attr) {
            return Value.createFromBlock(Block.createBlock(0, Cadmium.createObject(n)));
        } else if (n instanceof CDATASection) {
            return Value.createFromBlock(Block.createBlock(1, Cadmium.createObject(n)));
        } else if (n instanceof Comment) {
            return Value.createFromBlock(Block.createBlock(2, Cadmium.createObject(n)));
        } else if (n instanceof Document) {
            return Value.createFromBlock(Block.createBlock(3, Cadmium.createObject(n)));
        } else if (n instanceof DocumentType) {
            return Value.createFromBlock(Block.createBlock(4, Cadmium.createObject(n)));
        } else if (n instanceof Element) {
            return Value.createFromBlock(Block.createBlock(5, Cadmium.createObject(n)));
        } else if (n instanceof Entity) {
            return Value.createFromBlock(Block.createBlock(6, Cadmium.createObject(n)));
        } else if (n instanceof EntityReference) {
            return Value.createFromBlock(Block.createBlock(7, Cadmium.createObject(n)));
        } else if (n instanceof Notation) {
            return Value.createFromBlock(Block.createBlock(8, Cadmium.createObject(n)));
        } else if (n instanceof ProcessingInstruction) {
            return Value.createFromBlock(Block.createBlock(9, Cadmium.createObject(n)));
        } else if (n instanceof Text) {
            return Value.createFromBlock(Block.createBlock(10, Cadmium.createObject(n)));
        } else {
            Fail.failWith("unknown node type");
            return Value.UNIT; // never reached
        } // end if/elsif/else
    } // end method 'createNode(Node)'

} // end class 'Documents'
